	.module main.c
	.area text
_main::
; /**
;  * Timings with crystal oscillator 32768 Hz:
;  *   bus clock: 7340032 Hz
;  *   I2C: 101945 Hz
;  */     
; 
; #include <io08GB60.h>
; 
; #define NULL 0
; typedef unsigned char ui8;
; 
; // ---------------------------------------------------------------------------
; // Main
; // ---------------------------------------------------------------------------
; 
; void main (void) 
; {      
; 	// system initialization
; 	SOPT = 34;
	lda #34
	sta 0x1802
; 	SPMSC1 = 20;
	lda #20
	sta 0x1809
; 	SPMSC2 = 0;
	clra
	sta 0x180a
; 	ICGC1 = 56;
	lda #56
	sta 0x48
; 	ICGC2 = 217;
	lda #-39
	sta 0x49
L2:
L3:
; 	while (!(ICGS1 & 8));                  
	brclr #3,0x4a,L2
; 	FSTAT = 16;
	lda #16
	sta 0x1825
; 	PTFDD = 0xff;
	lda #-1
	sta 0x43
; 	IICC = 0;
	clr 0x5a
; 
; 	// modulo for 64 Hz interrupt
; 	TPM2MODH = (2*448) / 256;
	lda #3
	sta 0x63
; 	TPM2MODL = (2*448) & 0xff;
	lda #-128
	sta 0x64
; 
; 	// counter driven by bus clock, enable interrupt, prescale 128
; 	TPM2SC = 8 | 64 | 4 | 2 | 1;
	lda #79
	sta 0x60
	bra L6
L5:
	wait
L6:
;     
; 	while (1) asm("wait");
	bra L5
X0:
L1:
	.dbline 0 ; func end
	rts
;              i -> 0,X
;              n -> 4,X
_delayLoop:
	ais #-2
	tsx
; }
; 
; static void delayLoop(int n)
; {
	clr 1,X
	clr ,X
	bra L12
L9:
	nop
L10:
	lda 1,X
	add #1
	sta 1,X
	lda ,X
	adc #0
	sta ,X
L12:
; 	int i;
; 	for (i = 0; i < n; i++) asm("nop");
	lda 5,X
	sta *__r1
	lda 4,X
	sta *__r0
	lda 1,X
	sta *__r3
	lda ,X
	sta *__r2
	ldhx *__r2
	cphx *__r0
	tsx
	blt L9
L8:
	ais #2
	.dbline 0 ; func end
	rts
	.area data
_g_state::
	.blkb 1
	.area idata
	.byte 0
	.area data
_g_counter::
	.blkb 1
	.area idata
	.byte 0
	.area data
	.area text
_stopI2C:
; }
; 
; ui8 g_state = 0;
; ui8 g_counter = 0;
; 
; static void stopI2C()
; {
; 	// send stop bit and disable I2C
; 	IICC = 128 | 16;
	lda #-112
	sta 0x5a
;     delayLoop(5);
	ldhx #5
	pshx
	pshh
	jsr _delayLoop
	ais #2
	tsx
; 	IICC = 0;
	clr 0x5a
L13:
	.dbline 0 ; func end
	rts
	.area data
_g_i2cState::
	.blkb 1
	.area idata
	.byte 0
	.area data
_test::
	.blkb 1
	.area idata
	.byte 0
	.area data
	.area text
;          state -> 2,X
_i2cInterrupt::
	pshh
	lda *__r0
	psha
	lda *__r1
	psha
	lda *__r2
	psha
	lda *__r3
	psha
	lda *__r4
	psha
	lda *__r5
	psha
	lda *__r6
	psha
	lda *__r7
	psha
	lda *__r8
	psha
	lda *__r9
	psha
	lda *__r10
	psha
	lda *__r11
	psha
	lda *__rhi
	psha
	lda *__rlo
	psha
	ais #-3
	tsx
; }
; 
; ui8 g_i2cState = 0;
; ui8 test = 0;
; 
; #pragma interrupt_handler i2cInterrupt
; void i2cInterrupt(void)
; {
; 	// get state
; 	ui8 state = IICS;
	lda 0x5b
	sta 2,X
; 	
; 	// clear IICIF	
; 	IICS = 2;
	lda #2
	sta 0x5b
; 	  
; 	// wait until transfer complete
; 	if (!(state & 0x80)) return;
	lda 2,X
	bit #-128
	bne L15
	jmp L14
L15:
	clr ,X
	lda _g_i2cState
	sta 1,X
	lda ,X
	bne X1
	lda 1,X
	bne X12
	jmp L20
X12:
X1:
	lda ,X
	bne X2
	lda 1,X
	cmp #1
	bne X13
	jmp L21
X13:
X2:
	lda ,X
	bne X3
	lda 1,X
	cmp #2
	beq L22
X3:
	lda ,X
	bne X4
	lda 1,X
	cmp #3
	bne X14
	jmp L23
X14:
X4:
	lda ,X
	bne X5
	lda 1,X
	cmp #4
	bne X15
	jmp L24
X15:
X5:
	lda ,X
	bne X6
	lda 1,X
	cmp #5
	bne X16
	jmp L25
X16:
X6:
	lda ,X
	bne X7
	lda 1,X
	cmp #6
	bne X17
	jmp L26
X17:
X7:
	lda ,X
	bne X8
	lda 1,X
	cmp #7
	bne X18
	jmp L27
X18:
X8:
	lda ,X
	bne X9
	lda 1,X
	cmp #8
	bne X19
	jmp L28
X19:
X9:
	lda ,X
	bne X10
	lda 1,X
	cmp #9
	bne X20
	jmp L29
X20:
X10:
	jmp L17
X11:
; 
; 	// send next ui8 or stop bit
;     switch (g_i2cState) {
L20:
; 		case 0:  // write config command
;         	IICD = 7;
	lda #7
	sta 0x5c
;             break;
	jmp L18
L21:
; 		case 1:  // write config data
;         	IICD = 0;
	clr 0x5c
;             break;
	bra L18
L22:
; 		case 2:  // send start, stop and address 
; 			IICC = 128 | 64 | 16;  // stop
	lda #-48
	sta 0x5a
;             delayLoop(5);
	ldhx #5
	pshx
	pshh
	jsr _delayLoop
	ais #2
	tsx
; 			IICC = 128 | 64 | 32 | 16;  // start
	lda #-16
	sta 0x5a
; 			IICS = 2;
	lda #2
	sta 0x5b
; 			IICD = 0x26 << 1;
	lda #76
	sta 0x5c
;             break;
	bra L18
L23:
; 		case 3:  // write port 1 out command
;         	IICD = 3;
	lda #3
	sta 0x5c
;             break;
	bra L18
L24:
; 		case 4:  // write data to port 1
;         	IICD = test;
	lda _test
	sta 0x5c
;             break;
	bra L18
L25:
; 		case 5:  // send start, stop and address 
; 			IICC = 128 | 64 | 16;  // stop
	lda #-48
	sta 0x5a
;             delayLoop(5);
	ldhx #5
	pshx
	pshh
	jsr _delayLoop
	ais #2
	tsx
; 			IICC = 128 | 64 | 32 | 16;  // start
	lda #-16
	sta 0x5a
; 			IICS = 2;
	lda #2
	sta 0x5b
; 			IICD = 0x26 << 1;
	lda #76
	sta 0x5c
;             break;
	bra L18
L26:
; 		case 6:  // write read port 0 command
;         	IICD = 0;
	clr 0x5c
;             break;
	bra L18
L27:
; 		case 7:  // send repeated start and address with read mode bit
; 			IICC = 128 | 64 | 32 | 16 | 4;
	lda #-12
	sta 0x5a
; 			IICD = (0x26 << 1) | 1;
	lda #77
	sta 0x5c
;             break;
	bra L18
L28:
; 		case 8:  // switch to read mode
; 			IICC = 128 | 64 | 32 | 8;  // receive mode
	lda #-24
	sta 0x5a
;             state = IICD;  // dummy read to start read process
	lda 0x5c
	sta 2,X
;             break;
	bra L18
L29:
; 		case 9:  // display the received byte and stop I2C
;         	PTFD = IICD;
	mov *92,0x40
; 			IICC = 0;
	clr 0x5a
;             break;
L17:
L18:
; 	}
;     g_i2cState++;
	lda _g_i2cState
	add #1
	sta _g_i2cState
L14:
	ais #3
	.dbline 0 ; func end
	pula
	sta *__rlo
	pula
	sta *__rhi
	pula
	sta *__r11
	pula
	sta *__r10
	pula
	sta *__r9
	pula
	sta *__r8
	pula
	sta *__r7
	pula
	sta *__r6
	pula
	sta *__r5
	pula
	sta *__r4
	pula
	sta *__r3
	pula
	sta *__r2
	pula
	sta *__r1
	pula
	sta *__r0
	pulh
	rti
_timerInterrupt::
	pshh
; }
; 
; #pragma interrupt_handler timerInterrupt
; void timerInterrupt(void) 
; {
; 	// clear interrupt flag
; 	TPM2SC = TPM2SC & 0x7f;
	lda 0x60
	and #127
	sta 0x60
; 
; 	// start I2C
; 	IICF = 0x13;
	lda #19
	sta 0x59
; 	IICC = 128 | 64 | 16;
	lda #-48
	sta 0x5a
; 	IICC = 128 | 64 | 32 | 16;
	lda #-16
	sta 0x5a
; 	IICS = 2;
	lda #2
	sta 0x5b
;     g_i2cState = 0;
	clra
	sta _g_i2cState
; 	IICD = 0x26 << 1;
	lda #76
	sta 0x5c
; 
;     // increment counter with 8 Hz
;     if (g_counter > 0) {
	lda _g_counter
	cmp #0
	bls L31
;     	g_counter--;
	lda _g_counter
	sub #1
	sta _g_counter
	bra L32
L31:
;     } else {
;     	g_counter = 8;
	lda #8
	sta _g_counter
;         test++;
	lda _test
	add #1
	sta _test
; 	}
L32:
L30:
	.dbline 0 ; func end
	pulh
	rti
	.area memory(abs)
	.org 0xffc0
__vectab::
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word _i2cInterrupt
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word _timerInterrupt
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.word 0
	.area lit
